package com.yst.shader.feature.widget;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.view.View;

/**
 * @author: xuyujian@ysten.com
 * @date: 2020/8/18 9:23
 */
public class FocusHelper {


    private static int[] highLightColors = new int[]{0x4cffffff, 0xffffff};
    private static Matrix localMatrix = new Matrix();

    private static Xfermode mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);

    private static int ANI_TAG = -90011245;
    private static float animateValue = 0f;

    public static void addFocusShadow(View focusView, Canvas canvas, Paint paint, Bitmap drawCache, Bitmap focus, String extraText) {
        if (drawCache != null && !drawCache.isRecycled()) {
            //view rect
            Rect dst = new Rect(0, 0, focusView.getWidth(), focusView.getHeight());

            Rect drawCacheRect = new Rect((focusView.getWidth() - drawCache.getWidth()) / 2,
                    (focusView.getHeight() - drawCache.getHeight()) / 2,
                    (focusView.getWidth() - drawCache.getWidth()) / 2 + drawCache.getWidth(),
                    (focusView.getHeight() - drawCache.getHeight()) / 2 + drawCache.getHeight()
            );
            canvas.drawBitmap(focus, null, dst, paint);
            int layer = canvas.saveLayer(new RectF(dst), paint, Canvas.ALL_SAVE_FLAG);
            canvas.drawBitmap(drawCache, null, drawCacheRect, paint);
            paint.setShader(newLinearGradient(30f, highLightColors));
            canvas.setMatrix(localMatrix);
            localMatrix.setTranslate(focusView.getWidth() * animateValue, 0);
            localMatrix.postRotate(45f, focusView.getWidth() / 2f, focusView.getHeight() / 2f);
            newAnimator(focusView);
            if (animateValue > 0f){
                paint.setXfermode(mode) ;
                canvas.drawRect(0f, 0f, focusView.getWidth(), focusView.getHeight(), paint);
            }
            canvas.setMatrix(null);
            paint.setXfermode(null) ;
            paint.setShader(null) ;
            canvas.restoreToCount(layer);
        }
    }

    /**
     * (0,0)->(0,400)从上到下
     * (0,400)->(0,0) 从下到上
     * <p>
     * 0,0）->(getMeasuredWidth(),0) 表示从左到右
     * (getMeasuredWidth(),0)->(0,0) 表示从右到左
     * <p>
     * 0,0）-> (getMeasuredWidth(),getMeasuredHeight()) 斜角，从左上角到右下角
     *
     * @param colors
     * @return
     */
    private static LinearGradient newLinearGradient(float width, int[] colors) {
        return new LinearGradient(
                0f, 0f,
                width, 0f,
                colors, null,
                Shader.TileMode.CLAMP
        );
    }

    private static ValueAnimator newAnimator(View focus) {
        ValueAnimator tag = (ValueAnimator) focus.getTag(ANI_TAG);
        if (tag == null) {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 1f);

            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    animateValue = (float) animation.getAnimatedValue();
                    focus.invalidate();
                }
            });
            focus.setTag(ANI_TAG, animator);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.setRepeatMode(ValueAnimator.RESTART);
            animator.setDuration(3000);
            tag = animator;
        }

        assert tag != null;
        if (!tag.isStarted()) {
            tag.cancel();
            tag.setStartDelay(300);
            tag.start();
        }


        return tag;

    }


}
